let requestUrl = null;

function addHandler() {
    requestUrl = "/gang_skill_category/insert";

    $("#skill_dialog").dialog("open");
}

function editHandler() {
    let rowData = $("#gang_skill_list").datagrid("getSelected");

    if (rowData) {
        requestUrl = "/gang_skill_category/updateById";

        $("#id").val(rowData.id);
        $("#name").textbox("setValue", rowData.name);
        $("#note").textbox("setValue", rowData.note);
        $("#effect").textbox("setValue", rowData.effect);
        $("#score").numberspinner("setValue", rowData.score);
        $("#grade").numberspinner("setValue", rowData.grade);

        $("#skill_dialog").dialog("open");
    } else {
        alertMsg("请选择要修改的记录！", "warning");
    }
}

function imageHandler() {
    let rowData = $("#gang_skill_list").datagrid("getSelected");

    if (rowData) {
        $("#id_").val(rowData.id);
        $("#img").attr("src", rowData.image);

        $("#upload_dialog").dialog("open");
    } else {
        alertMsg("请选择要修改的记录！", "warning");
    }
}

$(document).ready(function() {

    $("#name").textbox({
        width: 120,
        required: true
    });

    $("#score").numberspinner({
        min: 0,
        max: 1,
        width: 120,
        required: true
    });

    $("#grade").numberspinner({
        min: 10,
        max: 20,
        width: 120,
        increment: 10,
        required: true
    });

    $("#note").textbox({
        width: 320,
        height: 60,
        required: true,
        multiline: true
    });

    $("#effect").textbox({
        width: 320,
        height: 60,
        required: true,
        multiline: true
    });

    $("#skill_dialog").dialog({
        title: "帮派技能",
        width: 440,
        buttons: [{
            iconCls: "icon-save",
            text: "保存",
            handler: function() {
                let selector = "#skill_form";

                checkForm(selector, function () {
                    let data = $(selector).serialize();

                    ajaxPost(requestUrl, data, function(resp) {
                        showMsg(resp.message);

                        $(selector).form("clear");
                        $("#skill_dialog").dialog("close");
                        $("#gang_skill_list").datagrid("reload");
                    }, error);
                });
            }
        }, {
            iconCls: "icon-cancel",
            text: "取消",
            handler: function() {
                $("#skill_dialog").dialog("close");
                $("#skill_form").form("clear");
            }
        }]
    });

    /******************************************************************************************************/

    fetchUploadFilebox("#gang_skill_list", "/gang_skill_category/upload");

    fetchUploadDialog("/gang_skill_category/updateImageById", function () {
        $("#gang_skill_list").datagrid("reload");
    });

    /******************************************************************************************************/

    /**
     * 表格体的高度
     * @type {number}
     */
    let heigt_body = heigt_tr_with_image * 10;
    /**
     * 表格高度
     * @type {number}
     */
    let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

    $("#gang_skill_list").datagrid({
        url: "/gang_skill_category/selectByPage",
        method: "get",
        height: height_table,
        fitColumns: true,
        rownumbers: true,
        pagination: true,
        toolbar: [{
            iconCls: "icon-add",
            text: "添加",
            handler: function() {
                addHandler();
            }
        }, "-", {
            iconCls: "icon-edit",
            text: "修改",
            handler: function() {
                editHandler();
            }
        }, "-", imageUpdateButton],
        columns: [[
            {field: "name", title: "技能名称", align: "center", width: 50},
            {field: "score", title: "技能评分", align: "center", width: 50},
            {field: "grade", title: "等级上限", align: "center", width: 50},
            {field: "image", title: "图片", align: "center", width: 50
                , formatter: function(value) {
                    return "<img height='" + size + ";' src='" + value + "' />";
                }
            },
            {field: "note", title: "技能说明", align: "center", width: 100
                , formatter: function(value) {
                    return "<div class='ell' title='" + value + "'>" + value + "</div>";
                }
            },
            {field: "effect", title: "技能效果", align: "center", width: 100
                , formatter: function(value) {
                    return "<div class='ell' title='" + value + "'>" + value + "</div>";
                }
            }
        ]]
    });

});